<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\WriteQuestion;

/**
 * WriteQuestionSearch represents the model behind the search form about `app\models\WriteQuestion`.
 */
class WriteQuestionSearch extends WriteQuestion
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'tnum', 'class1_id', 'class2_id', 'num'], 'integer'],
            [['info', 'question', 'reading_content', 'url', 'hearing_content', 'diff'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = WriteQuestion::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'tnum' => $this->tnum,
            'class1_id' => $this->class1_id,
            'class2_id' => $this->class2_id,
            'num' => $this->num
        ]);

        $query->andFilterWhere(['like', 'info', $this->info])
            ->andFilterWhere(['like', 'question', $this->question])
            ->andFilterWhere(['like', 'reading_content', $this->reading_content])
            ->andFilterWhere(['like', 'url', $this->url])
            ->andFilterWhere(['like', 'hearing_content', $this->hearing_content])
            ->andFilterWhere(['like', 'diff', $this->diff]);

        return $dataProvider;
    }
}
